package Lee_code;

import java.util.Arrays;

/**
 * @author blh
 * @date 2024年06月30日 19:04
 */
public class Lee_34_630 {
    public int[] searchRange(int[] nums, int target) {
        int len = nums.length;
        // 思路: 二分查找
        int fast = len - 1;
        int slow = 0;
        int[] res = new int[2];
        Arrays.fill(res, -1);
        // 先找最右边的
        while (fast >= slow) {
            int mid = slow + (fast - slow) / 2;
            if (nums[mid] == target) {
                res[1] = mid;
                slow = mid + 1;
            } else if (nums[mid] > target) {
                fast = mid - 1;
            } else {
                slow = mid + 1;
            }
        }
        fast = len - 1;
        slow = 0;
        // 先找最左边的
        while (fast >= slow) {
            int mid = slow + (fast - slow) / 2;
            if (nums[mid] == target) {
                res[0] = mid;
                fast = mid - 1;
            } else if (nums[mid] > target) {
                fast = mid - 1;
            } else {
                slow = mid + 1;
            }
        }
        return res;
    }
}
